package at.fhooe.mcm.lri.mdmpoc.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;



@SuppressWarnings("serial")
public class HelloWorldServlet extends HttpServlet {
	
	//private static final Logger log = Logger.getAnonymousLogger();
	private static final Logger log = Logger.getLogger(HelloWorldServlet.class
			.getName());
	
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
//		log.log(Level.INFO, "HelloWorldServlet: enter service method ....");
//		PrintWriter writer = resp.getWriter();
//		writer.print("<h1>Hello, world!</h1>");
//		writer.close();
//		log.log(Level.INFO, "HelloWorldServlet: exit service method ");

        UserService userService = UserServiceFactory.getUserService();

        String thisURL = req.getRequestURI();

		
		 resp.setContentType("text/html");
	        if (req.getUserPrincipal() != null) {
	            resp.getWriter().println("<p>Hello, " +
	                                     req.getUserPrincipal().getName() +
	                                     "!  You can <a href=\"" +
	                                     userService.createLogoutURL(thisURL) +
	                                     "\">sign out</a>.</p>");
	        } else {
	            resp.getWriter().println("<p>Please <a href=\"" +
	                                     userService.createLoginURL(thisURL) +
	                                     "\">sign in</a>.</p>");
	        }

		
	}


}
